package playground;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.farng.mp3.MP3File;

import com.echonest.api.v4.EchoNestAPI;
import com.echonest.api.v4.EchoNestException;
import com.echonest.api.v4.Params;
import com.echonest.api.v4.Song;

public class SongsNamedAfterAGirl {

	private EchoNestAPI echoNest;
	private HashMap <String, List<Song>> cache = new HashMap <String, List<Song>>();
	private int counter = 0;

	public SongsNamedAfterAGirl() throws EchoNestException {
		echoNest = new EchoNestAPI("QTJ926WQJ92B089WB");
		echoNest.setTraceSends(false);
		echoNest.setTraceRecvs(false);
	}

	public void analyzeListOfNames(String filename){

		List<Song> songsNamedAfterThisGirl = null;

		try {
			BufferedReader in = new BufferedReader(new FileReader(filename));
			String zeile = null;
			while ((zeile = in.readLine()) != null) {
				try {
					songsNamedAfterThisGirl = searchAllSongsByTitle(zeile);
				} catch (EchoNestException e) {
					e.printStackTrace();
				}

				for (int i = 0; i < songsNamedAfterThisGirl.size(); ++i){
					Song s = songsNamedAfterThisGirl.get(i);
					if (s.getTitle().equals(zeile)){
						++counter;
					}
				}
				
				System.out.println(zeile + " " + counter);
				counter = 0;
				

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	


	public List<Song> searchAllSongsByTitle(String title)
	throws EchoNestException {
		Params p = new Params();
		p.add("title", title);
		p.add("results", 100);
		if (cache.containsKey(title)){
			return cache.get(title);
		}
		
		else{
			
			List<Song> songs = echoNest.searchSongs(p);
			cache.put(title, songs);
			return songs;
			
		}

	}

	public static void main(String[] args) throws EchoNestException {
		
		System.out.println("--- Songs Named After A Girl ----");
		System.out.println("Only songs with the exact title are counted. For example 'Jenny' counts as a song, 'oh Jenny' does not.");
		System.out.println("");

		


		SongsNamedAfterAGirl snag = new SongsNamedAfterAGirl();
		snag.analyzeListOfNames("stuff/popular_girl_names.txt");


	}




}
